package com.joplus.housekeeper.ui.view.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.adapter.PhoneAreaAdapter;
import com.joplus.housekeeper.bean.PhoneAreaBean;
import com.joplus.housekeeper.utils.DensityUtils;

/**
 * Created by ccm on 2016/7/5 14:14
 */
public class PhoneAreaDialog extends PopupWindow {


    private View mMenuView;
    private Context mContext;
    private AreaClick areaClick;

    private final ListView lv;
    PhoneAreaBean phoneAreaBean;
    int areaPosition = 0;

    public PhoneAreaDialog(Context context, final PhoneAreaBean phoneAreaBean, int areaPosition, final AreaClick areaClick) {
        super(context);
        mContext = context;
        this.phoneAreaBean = phoneAreaBean;
        this.areaPosition = areaPosition;
        this.areaClick = areaClick;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_phonearea, null);

        lv = (ListView) mMenuView.findViewById(R.id.lv);
        PhoneAreaAdapter phoneAreaAdapter = new PhoneAreaAdapter(mContext, phoneAreaBean.getData());
        phoneAreaAdapter.setPosition(areaPosition);
        lv.setAdapter(phoneAreaAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                areaClick.area(position, phoneAreaBean.getData().get(position).getCode());
                dismiss();
            }
        });

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高

        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimTop4area);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
     /*   mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.share_ll_menu).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        //  dismiss();
                    }
                }
                return true;
            }
        });*/


    }


    public void show(View view, View view2) {
        setHeight(DensityUtils.dip2px(mContext, 335));
        showAsDropDown(view, 0, 0);

    }

    public interface AreaClick {
        public void area(int position, String code);
    }

}
